package de.mhus.lib.mandala.demo;

import java.util.List;

import org.codehaus.jackson.JsonNode;

import de.mhus.lib.mandala.Function;
import de.mhus.lib.mandala.Window;
import de.mhus.lib.mandala.attributes.NamedAttribute;
import de.mhus.lib.mandala.bind.DataProvider;
import de.mhus.lib.mandala.bind.DataRow;
import de.mhus.lib.mandala.bind.DynamicDataSource;
import de.mhus.lib.mandala.functions.EncapsulatedFunctionCall;
import de.mhus.lib.mandala.functions.FunctionCall;
import de.mhus.lib.mandala.layout.HorizontalLayout;
import de.mhus.lib.mandala.layout.Panel;
import de.mhus.lib.mandala.list.ListItem;
import de.mhus.lib.mandala.list.SelectableList;
import de.mhus.lib.mandala.table.DynamicTable;
import de.mhus.lib.mandala.table.TableColumn;
import de.mhus.lib.mandala.text.Text;

public class Demo01 extends Demo {

	protected void create(Window window) {
		
		HorizontalLayout layout = window.add(new HorizontalLayout());
		Panel panel = layout.add(new Panel("List"), new HorizontalLayout.Options(3));
		
		SelectableList list = panel.add(new SelectableList());
		ListItem item = list.setItem(new ListItem());
		Text heading = new Text(Text.TYPE.H4,"");
		heading.getTagAttributes().setBindValue(new NamedAttribute("name"));
		item.add(heading);

		DynamicDataSource userListData = new DynamicDataSource();
		list.getTagAttributes().setData(userListData.getTarget());
		userListData.setProvider(new DataProvider() {
			
			@Override
			public void fillWithData(JsonNode in, java.util.List<DataRow> data) {
				data.add(new DataRow("name", "Hans","id",1));
				data.add(new DataRow("name", "Peter","id",2));
				data.add(new DataRow("name", "Lars","id",3));
				data.add(new DataRow("name", "Sabine","id",4));
				data.add(new DataRow("name", "Max","id",5));
			} 
		});
		
		//list.setSelectAction(new EncapsulatedFunctionCall("data,event", new Function("alert(data);"), "(JSON.stringify(data));" ) );
		item.getTagAttributes().setClickAction(new EncapsulatedFunctionCall("data,event", new Function("alert(data);"), "(JSON.stringify(data));" ) );

		window.getInitialAction().getActions().add(userListData);
		
		// ---
		
		Panel panelr = layout.add(new Panel("Table"), new HorizontalLayout.Options(9));
		
		DynamicTable table = panelr.add(new DynamicTable());
		
		table.addColumn(new TableColumn("#", "a", "nr"));
		table.addColumn(new TableColumn("First Name", "b", "first"));
		table.addColumn(new TableColumn("Last Name", "c", "last"));
		table.addColumn(new TableColumn("Username", "d", "user"));

		DynamicDataSource userListData2 = new DynamicDataSource();
		table.getTagAttributes().setData(userListData2.getTarget());
		userListData2.setProvider(new DataProvider() {

			@Override
			public void fillWithData(JsonNode in, List<DataRow> data) {
				data.add(new DataRow("nr", 1, "first", "Hans", "last", "Otto", "user","@mdo"));
				data.add(new DataRow("nr", 2, "first", "Hans", "last", "Otto", "user","@mdo"));
			}
			
		});
		
		window.getInitialAction().getActions().add(userListData2);
		
	}
	
}
